var Module = angular.module('ngApp', []);
Module.controller('mainCtrl',function ($scope,$http) {
    $scope.server = "http://test.wechat.inuorui.com/api/data"
    $scope.title = '详情页'
    $scope.type= null
    $scope.shipData = null
    $scope.redata = {}
    $scope.modalContent = ''
    $scope.detailModal = function (item) {
        if($scope.type ===2){
            $scope.modalContent =$scope.server + "/checkIn/cctv/image?name=" + item
        }
        else if($scope.type ===1){
            $scope.modalContent =item;
        }
        else{
            $scope.modalContent =item;
        }
    }
    /**
     * 格式化详细内容
     * @param msg
     * @returns {string}
     */
    $scope.formatMsg = function (msg) {
        if (msg && msg.length > 15) {
            return msg.substr(0, 15) + '...';
        }
        return msg;
    }
    /**
     * 获取全部船舶信息
     */
    $scope.queryShips = function () {
        $http({
            method: "get",
            url: $scope.server + "/checkIn/ship",

        }).then(function (res) {
            if (res.data.reMsg === 'SUCCESS') {
                $scope.shipData= res.data.reData;
                $scope.shipData[0] = {mmsi:-1,name:'全部船舶'};
                if ($scope.shipData === null || $scope.shipData.length === 0) {
                    alert("没有查询到相关数据，请稍后再试");
                }
            }
            else {
                alert("服务器异常，请稍后再试");
            }
        }).catch(function (e) {
            console.error(e);
            alert("网络异常，请检查网络后，再试");
        });
    }
    /**
     * 根据mmsi获取对应的船名
     */
    $scope.formatShipName = function (mmsi) {
        for (var index in $scope.shipData){
            if($scope.shipData[index].mmsi === mmsi){
                return $scope.shipData[index].name;
            }
        }
    }

    var getQueryString = function (name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    };
    var getAPI = function (param) {
        if(param == 'checkin'){
            return '/checkIn/record/queryById'
        }
        else if(param ==1){
            return '/checkIn/exception/queryById'
        }
        else if(param ==2){
            return '/checkIn/cctv/query'
        }

    }
    var getData = function () {
        $http({
            method: "get",
            url: $scope.server + getAPI($scope.type),
            params: {
               "id":$scope.id
            }
        }).then(function (res) {
            if (res.data.reMsg === 'SUCCESS') {
                $scope.redata = res.data.reData;
                for (var index in $scope.redata){
                    $scope.redata[index].shipName = $scope.formatShipName($scope.redata[index].mmsi);
                }
                if ($scope.redata === null || $scope.redata.length === 0) {
                    alert("没有查询到相关数据，请稍后再试");
                }
                $scope.totalPages = res.data.reData.totalPages;
            }
            else {
                alert("服务器异常，请稍后再试");
            }
        }).catch(function (e) {
            console.error(e);
            alert("网络异常，请检查网络后，再试");
        });
    }
    var init = function(){
        $scope.type = getQueryString("type")
        $scope.id = getQueryString("id")
        getData();
        $scope.queryShips();
    }
    init();

})